﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IOBusiness;
using IOEntity;
using System.Data;

namespace Indochinaodyssey.Admin
{
    public partial class QLAlbum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("noidung"))
                {
                    Response.Redirect("Detail.aspx?tourid=" + e.CommandArgument.ToString());
                }
                else
                {
                    if (e.CommandName.Equals("anh"))
                    {
                        Response.Redirect("HinhAnh.aspx?loaiid=1&tourid=" + e.CommandArgument.ToString());
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            mtvTour.ActiveViewIndex = 1;
            clsTour tl = new clsTour();
            tl.MyTheLoai = Convert.ToInt32(cbxTheLoai.SelectedValue);
            tl.TieuDe = txtTieuDe.Text;
            tl.GhiChu = txtGhiChu.Value;
            tl.Hot = ckbHot.Checked;
            tl.Stt = Convert.ToInt32(txtViTri.Text);
            if (fileUpload.HasFile)
            {
                tl.HinhAnh = fileUpload.FileName;

            }
            else
            {
                tl.HinhAnh = "demo.jpg";
            }

            clsBSTour bsTL = new clsBSTour();

            if (bsTL.Insert(tl))
            {
                if (fileUpload.HasFile == true)
                {
                    String file = fileUpload.FileName;
                    fileUpload.SaveAs(Server.MapPath("~/Images/HinhAnh/") + file);
                }

                lblMes.Text = "<script>alert('Tour đã được tạo');</script>";
                mtvTour.ActiveViewIndex = 0;
                GridView1.DataBind();
            }
            else
            {
                lblMes.Text = "<script>alert('Lỗi, vui lòng kiểm tra dữ liệu !');</script>";
            }
        }

        protected void btnEditT_Click(object sender, EventArgs e)
        {
            mtvTour.ActiveViewIndex = 2;
            LinkButton lbt = (LinkButton)sender;
            HiddenField h = (HiddenField)lbt.FindControl("hfID");
            hfID.Value = h.Value;
            clsBSDungChung c = new clsBSDungChung();
            string sql = "Select t.*,case when t.hot is NULL THEN 0  when t.Hot=0 then 0 else 1 END AS Hot2 from tblTour t where t.ID=" + h.Value;
            DataTable tb = c.GetDataTableSQL(sql, "TheLoai");
            cbxTheLoaiEdit.SelectedValue = tb.Rows[0]["TheLoaiID"].ToString();
            txtTieuDeEdit.Text = tb.Rows[0]["TieuDe"].ToString();
            txtViTriEdit.Text = tb.Rows[0]["STT"].ToString();
            hfImage.Value = tb.Rows[0]["HinhAnh"].ToString();
            if (tb.Rows[0]["HinhAnh"].ToString() == "")
            {
                imageTour.Visible = false;

            }
            else
            {
                imageTour.Visible = true;
                imageTour.ImageUrl = "~/Images/HinhAnh/" + tb.Rows[0]["HinhAnh"].ToString();
            }
            if (tb.Rows[0]["Hot2"].ToString() == "0")
            {
                ckbHotEdit.Checked = false;
            }
            else
            {
                ckbHotEdit.Checked = true;
            }
            fileUploadEdit.Visible = false;
            btnIGM.Visible = true;
            txtGhiChuEdit.Value = tb.Rows[0]["GhiChu"].ToString();

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            LinkButton lbt = (LinkButton)sender;
            HiddenField h = (HiddenField)lbt.FindControl("hfID");
            clsTour tl = new clsTour();
            tl.Id = Convert.ToInt32(h.Value);
            clsBSTour t = new clsBSTour();
            if (t.Del(tl))
            {
                lblMes.Text = "<script>alert('Tour đã được xóa');</script>";
                mtvTour.ActiveViewIndex = 0;
                GridView1.DataBind();
            }
            else
            {
                lblMes.Text = "<script>alert('Lỗi, không thể xóa !');</script>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            mtvTour.ActiveViewIndex = 0;
            lblMes.Text = "";
            btnIGM.Visible = true;
            fileUploadEdit.Visible = false;
        }

        protected void btnCre_Click(object sender, EventArgs e)
        {
            mtvTour.ActiveViewIndex = 1;
        }

        protected void btnEditSave_Click(object sender, EventArgs e)
        {
            mtvTour.ActiveViewIndex = 2;
            clsTour tl = new clsTour();
            tl.MyTheLoai = Convert.ToInt32(cbxTheLoaiEdit.SelectedValue);
            tl.TieuDe = txtTieuDeEdit.Text;
            tl.GhiChu = txtGhiChuEdit.Value;
            tl.Hot = ckbHotEdit.Checked;
            tl.Stt = Convert.ToInt32(txtViTriEdit.Text);
            tl.Id = Convert.ToInt32(hfID.Value);
            if (btnIGM.Visible == false)
            {
                if (fileUploadEdit.FileName == null)
                {
                    tl.HinhAnh = hfImage.Value;
                }
                else
                {
                    tl.HinhAnh = fileUploadEdit.FileName;
                    if (fileUploadEdit.HasFile)
                    {
                        fileUploadEdit.SaveAs(Server.MapPath("~/Images/HinhAnh/") + fileUploadEdit.FileName);
                    }

                }
            }
            else
            {
                tl.HinhAnh = hfImage.Value;
            }
            clsBSTour bsTL = new clsBSTour();

            if (bsTL.Edit(tl))
            {
                lblMes.Text = "<script>alert('Tour đã được sửa');</script>";
                mtvTour.ActiveViewIndex = 0;
                GridView1.DataBind();
            }
            else
            {
                lblMes.Text = "<script>alert('Lỗi, vui lòng kiểm tra dữ liệu !');</script>";
            }
        }

        protected void btnIGM_Click(object sender, EventArgs e)
        {
            fileUploadEdit.Visible = true;
            btnIGM.Visible = false;
            btnHuy.Visible = true;
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            fileUploadEdit.Visible = false;
            btnIGM.Visible = true;
            btnHuy.Visible = false;
        }
    }
}